//
//  WYLoginView.swift
//  weibo
//
//  Created by 王玉东 on 16/4/7.
//  Copyright © 2016年 wyd. All rights reserved.
//

import UIKit
import SnapKit


//MARK:设置代理
@objc protocol WYLoginViewDelegate : NSObjectProtocol {

 optional func login()
    
 optional func register()


}




class WYLoginView: UIView {
    
    
    var delegate: WYLoginViewDelegate?
    
    func setbase(text:String , bigImage:UIImage , isHome:Bool = false)
    {
        if isHome {
        
            staranimation()
        
        }else {
        
        
            self.bigImage.image = bigImage
            
            self.litterImage.hidden = true
            
            self.bringSubviewToFront(self.bigImage)
            
            self.loginLable.text = text
        
        }
    
    }
    
  private  func staranimation(){
    
    let anim = CABasicAnimation(keyPath: "transform.rotation")
    //设置时间
    anim.duration = 15
    //设置动画次数
    anim.repeatCount = MAXFLOAT

    anim.toValue = 2 * M_PI
    //设置当动画结束不移除
    anim.removedOnCompletion = false
    
    bigImage.layer.addAnimation(anim, forKey: nil)
    
    }
    
    lazy var bigImage: UIImageView = {
    
         let bigimageview = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
         return bigimageview

    }()
   
    lazy var litterImage: UIImageView = {
        
        let litterimageview = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return litterimageview
        
    }()
    
    lazy var loginLable:UILabel = {
        let l = UILabel()
        l.text = "如果没有注册请注册"
        l.textColor = UIColor.darkGrayColor()
        l.font = UIFont.systemFontOfSize(13)
        l.numberOfLines = 2
        l.textAlignment = .Center
        l.sizeToFit()
        return l
    }()
    
    lazy var cover:UIImageView = {
    
        let cover:UIImageView = UIImageView()
        
        cover.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
    
        cover.sizeToFit()
        
        return cover
    
    }()
    
    lazy var loginBtn: UIButton = {
    
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
       // btn.titleLabel?.textColor = UIColor.darkGrayColor()
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        btn.addTarget(self, action: "loginClick", forControlEvents: .TouchUpInside)
    
        return btn
    }()
    
    lazy var registerBtn: UIButton = {
        
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        btn.setTitle("注册", forState: .Normal)
        
        btn.addTarget(self, action: "registerClick", forControlEvents: .TouchUpInside)
        //设置
        return btn
    }()
    
    func registerClick() {
        
    //判断是否实现代理方法
    self.delegate?.register?()
    
    }
    
    func loginClick(){
    
        self.delegate?.login?()
    
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(bigImage)
        addSubview(cover)
        addSubview(litterImage)
        addSubview(loginLable)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
    
        super.layoutSubviews()
        
        loadImageView(bigImage)
        loadImageView(litterImage)
        loadOtherView()
    }
    
  //MARK:设置imagview的位置
    private func loadOtherView(){
        
        loginLable.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(bigImage.snp_bottom).offset(20)
            make.centerX.equalTo(self)
            make.width.equalTo(230)
            make.height.equalTo(40)
            
        }
        loginBtn.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(loginLable.snp_left)
    
            make.top.equalTo(loginLable.snp_bottom).offset(20)
            
            make.width.equalTo(100)
  
            
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            
            make.right.equalTo(loginLable.snp_right)
            
            make.top.equalTo(loginLable.snp_bottom).offset(20)
            
            make.width.equalTo(100)
            
        }
        
        cover.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(bigImage)
            
        }
    
    }
    
    
    
 private  func loadImageView(imageView:UIImageView) {
    
        imageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.snp_centerX)
            
            make.centerY.equalTo(self.snp_centerY).offset(-80)
            
    }
    
    }

}
